/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jefb.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.jefb.entity.RoleEntity;
import org.jefb.entity.UserEntity;
import org.jefb.sec.entity.UserRight;
import org.jefb.service.ICommonPersistenceService;
import org.jefb.service.IUserSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component(IUserSessionService.COMPONENT_NAME)
@Scope("session")
public class UserSessionService implements Serializable, IUserSessionService {
	
	private static final long serialVersionUID = 1L;

	private UserEntity userEntity;

	@Autowired
	private ICommonPersistenceService commonPersistenceService;

	public UserSessionService() {
	}
	
	public boolean getLoggedIn(){
		return !getLoginRequired();
	}

	public boolean getLoginRequired() {
		return SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal() == null
				|| SecurityContextHolder.getContext().getAuthentication()
						.getPrincipal().equals("anonymousUser");
	}

	public String getHomeDir() {
		if (getUserEntity() != null && getUserEntity().getHomeDir() != null) {
			return getUserEntity().getHomeDir();
		} else {
			return FileUtils.getUserDirectoryPath();
		}
	}

	public String getUsername() {
		return SecurityContextHolder.getContext().getAuthentication().getName();
	}

	public UserEntity getUserEntity() {
		if (userEntity == null) {
			String userName = SecurityContextHolder.getContext()
					.getAuthentication().getName();
			UserEntity example = new UserEntity();
			example.setUsername(userName);
			List<UserEntity> foundUsers = commonPersistenceService
					.findAllByExample(example);
			if (foundUsers != null && !foundUsers.isEmpty()) {
				userEntity = foundUsers.get(0);
			}
		}
		return userEntity;
	}

	public void setCommonPersistenceService(
			ICommonPersistenceService commonPersistenceService) {
		this.commonPersistenceService = commonPersistenceService;
	}

	public ICommonPersistenceService getCommonPersistenceService() {
		return commonPersistenceService;
	}

	public Collection<UserRight> getAllUserRights() {
		if (getUserEntity() != null && getUserEntity().getUserRights() != null) {
			Set<UserRight> allUserRights = new HashSet<UserRight>();
			allUserRights.addAll(userEntity.getUserRights());
			Collection<RoleEntity> userRoles = userEntity.getUserRoles();
			if (userRoles != null && !userRoles.isEmpty()) {
				for (RoleEntity userRole : userRoles) {
					allUserRights.addAll(userRole.getRights());
				}
			}
			return allUserRights;
		} else {
			return new HashSet<UserRight>();
		}
	}

	public boolean hasRight(UserRight rightToCheck) {
		if (getAllUserRights().contains(UserRight.ALL)) {
			return true;
		} else {
			return getAllUserRights().contains(rightToCheck);
		}
	}
}
